package com.hk.model;

import java.sql.SQLException;
import java.util.ArrayList;

import com.hk.dto.GsBoardDto;

public class GsBoardManager extends SqlDaoSuport implements IGsBoardManager {

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<GsBoardDto> getAllGsBoards() {
		
		ArrayList<GsBoardDto> list = new ArrayList<GsBoardDto>();
		try {
			list=(ArrayList<GsBoardDto>)getSqlMapClient().queryForList("getAllGsBoards");
		} catch (SQLException e) {			
			System.out.println(e);
		}
		return list;
	}

	@Override
	public boolean writeBoard(GsBoardDto gsdto) {
		int count=0;
		try {
			getSqlMapClient().insert("writeBoard", gsdto);
			count=1;
		} catch (SQLException e) {
			count=-1;
			System.out.println(count);
		}		
		return count>0?true:false;
	}

	@Override
	public GsBoardDto getDetailBoard(String seq) {
		GsBoardDto dto = new GsBoardDto();
		try {
			dto=(GsBoardDto)getSqlMapClient().queryForObject("getDetailBoard", seq);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dto;
	}

	@Override
	public boolean updateBoard(GsBoardDto udto) {
		boolean update=false;
		try {
			getSqlMapClient().update("updateBoard", udto);
			update=true;
		} catch (SQLException e) {
			System.out.println(update);
			update=false;
		}		
		return update;
	}	
	
	@Override
	public GsBoardDto getCustUser(String seq) {
		GsBoardDto dto = new GsBoardDto();
		try {
			dto=(GsBoardDto)getSqlMapClient().queryForObject("getCustUser", seq);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dto;
	}

	@Override
	public boolean addReply(GsBoardDto rdto) {
		int count=0;
		try {			
			getSqlMapClient().startTransaction();
			getSqlMapClient().update("updateBoardAsRep", rdto);
			getSqlMapClient().insert("addReply", rdto);			
			getSqlMapClient().commitTransaction();
			count=1;
		} catch (SQLException e) {			
			count=-1;
			System.out.println("addReply---->"+e);
		}finally{
			try{
				getSqlMapClient().endTransaction();
			}catch (Exception e) {
				System.out.println(e);
			}
		}		
		return count>0?true:false;
	}	
}
